import React, { Component } from "react";
import './TodoMain.css';

export default class TodoMain extends Component {

    render() {
        //获取所有的任务
        let {todos} = this.props;
        
        return (
            <ul className="todo-main">
                {
                    todos.map(item => {
                        return <li key={item.id}>
                                    <label>
                                        <input onChange={this.change(item.id)} checked={item.done} type="checkbox" />
                                        <span>{item.title}</span>
                                    </label>
                                    <button
                                        className="btn btn-danger"
                                        style={{ display: item.done ? 'block' : 'none' }}
                                        onClick={this.remove(item.id)}
                                    >
                                        删除
                                    </button>
                                </li>
                    })
                }

                {todos.length === 0 && <li className="empty">暂无任务</li>}
                
            </ul>
        );
    }

    change = (id) => {
        //调用父级传入的 checkTodo 修改状态
        return (e) => {
            // console.log(e.target.checked);
            this.props.checkTodo(id, e.target.checked);
        }
    }

    remove = (id) => {
        return () => {
            if(window.confirm('您确定要删除该任务么??')) {
                //删除
                this.props.removeTodo(id);
            }
        }
    }
    
}


